package com.atguigu.controller;

import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author chenxin
 * @date 2023/2/10
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController{
    @DubboReference
    private AdminService adminService;

    @RequestMapping
    public String findAdmins(HttpServletRequest request,Map map){
        //1. 从请求中获取所有的请求参数
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);
        //将数据放到请求域
        map.put("page",page);
        //数据回显
        map.put("filters",filters);
        return "admin/index";
    }

    @RequestMapping("/create")
    public String create(){
        return "admin/create";
    }

    @RequestMapping("/save")
    public String sava(Admin admin){
        adminService.insert(admin);
        return "common/successPage";
    }

    @RequestMapping("/edit/{adminId}")
    public String edit(@PathVariable Long adminId,Map map){
        Admin admin = adminService.getById(adminId);
        map.put("admin",admin);
        return "admin/edit";
    }

    @RequestMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return "common/successPage";
    }

    @RequestMapping("/delete/{adminId}")
    public String delete(@PathVariable Long adminId){
        adminService.delete(adminId);
        return "redirect:/admin";
    }
}